package com.cbgolf.oa.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

import com.cbgolf.oa.R;
import com.cbgolf.oa.entity.ParkBean;
import com.cbgolf.oa.util.TextUtil;
import com.cbgolf.oa.util.Util;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Created by lq on 2017/11/15.
 */

public class AutoTextViewAdapter extends BaseAdapter implements Filterable {

  private ArrayFilter mFilter;
  private Context context;
  private List<ParkBean> list;
  private ArrayList<ParkBean> mUnfilteredData;
  private int max = 8;
  private String name;

  public interface IAutoTextViewWorker {

    void onClick(String name, String id);
  }

  private IAutoTextViewWorker worker;

  public AutoTextViewAdapter(Context context, List<ParkBean> list, String name,
      IAutoTextViewWorker worker) {
    this.context = context;
    this.list = list;
    this.name = name;
    this.worker = worker;

  }

  public void clear() {
    if (!Util.listIsNull(list)) {
      list.clear();
    }
  }

  public void setList(List<ParkBean> list) {
    this.list = list;
    this.notifyDataSetChanged();
  }

  @Override
  public int getCount() {
    // TODO Auto-generated method stub
    return list == null ? 0 : list.size();
  }

  @Override
  public Object getItem(int position) {
    // TODO Auto-generated method stub
    return list.get(position);
  }

  @Override
  public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
  }

  @Override
  public View getView(final int position, View view, ViewGroup parent) {
    // TODO Auto-generated method stub
    Holder h;
    if (view == null) {
      h = new Holder();
      view = View.inflate(context, R.layout.item_autotv, null);
      h.tv = (TextView) view.findViewById(R.id.item_auto_tv);
      view.setTag(h);
    } else {
      h = (Holder) view.getTag();
    }

    h.tv.setText(TextUtil.Text(list.get(position).courseName));
    h.tv.setOnClickListener(v -> {
      // TODO Auto-generated method stub
      if (null != worker) {
        worker.onClick(list.get(position).courseName, list.get(position).userAccount);
      }

    });
    return view;
  }

  class Holder {

    private TextView tv;
  }

  @Override
  public Filter getFilter() {
    if (mFilter == null) {
      mFilter = new ArrayFilter();
    }
    return mFilter;
  }

  private class ArrayFilter extends Filter {

    @SuppressLint("DefaultLocale")
    @Override
    protected FilterResults performFiltering(CharSequence prefix) {
      FilterResults results = new FilterResults();
      if (mUnfilteredData == null) {
        mUnfilteredData = new ArrayList<>(list);
      }
      if (prefix == null || prefix.length() == 0) {
        ArrayList<ParkBean> list = mUnfilteredData;
        results.values = list;
        results.count = list.size();
      } else {
        ArrayList<ParkBean> unfilteredValues = mUnfilteredData;
        int count = unfilteredValues.size();
        ArrayList<ParkBean> newValues = new ArrayList<ParkBean>(count);
        for (int i = 0; i < count; i++) {
          ParkBean info = unfilteredValues.get(i);
          if (null == info) {
            continue;
          }
          if (!Util.isNull(info.courseName) && (TextUtil.contains(info.courseName, name)
              || info.courseName.startsWith(name))) {
            newValues.add(info);
          }
          if (newValues.size() >= max) {
            break;
          }
        }
        results.values = newValues;
        results.count = newValues.size();
      }

      return results;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
      // noinspection unchecked
      list = (List<ParkBean>) results.values;
      if (results.count > 0) {
        notifyDataSetChanged();
        return;
      }
      notifyDataSetInvalidated();
    }

  }
}
